package com.article.service.impl;

import com.article.dao.TestTimeDao;
import com.article.dto.ReturnError;
import com.article.enums.ReturnInfoEnum;
import com.article.model.RoleInfo;
import com.article.model.TestTime;
import com.article.service.RoleInfoService;
import com.article.service.TestTimeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/10.
 */
@Service("testTimeService")
public class TestTimeServiceImpl implements TestTimeService {
    @Autowired
    private TestTimeDao testTimeDao;
    @Autowired
    private RoleInfoService roleInfoService;

    public Long countTotal(Map<String, Object> map) {
        return testTimeDao.countTotal(map);
    }

    public PageInfo<TestTime> listByPage(Integer pageIndex, Integer limit, Map<String, Object> map) {
        PageHelper.startPage(pageIndex, limit);
        List<TestTime> testTimeList = testTimeDao.listByPage(map);
        return new PageInfo<TestTime>(testTimeList);
    }

    public List<TestTime> listByMap(Map<String, Object> map) {
        return testTimeDao.listByMap(map);
    }

    @Transactional
    public ReturnError saveTestTime(TestTime testTime) {
        try {
            for (int i = 0; i < 10; i++) {
                testTime = new TestTime();
                testTime.setGmtCreate(Calendar.getInstance().getTime());
                testTimeDao.save(testTime);
                if (i < 3) {
                    RoleInfo roleInfo = new RoleInfo();
                    roleInfo.setRoleName("role" + i);
                    roleInfo.setGmtCreate(Calendar.getInstance().getTime());
                    roleInfo.setGmtModified(roleInfo.getGmtCreate());
                    roleInfoService.saveRole(roleInfo);
                }
                if (i == 5) {
                    i = i / 0;
                }
            }
            return new ReturnError(false, ReturnInfoEnum.SUCCESS.getType(), ReturnInfoEnum.SUCCESS.getInfo(), null);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public TestTime getById(Long id) {
        return testTimeDao.getById(id);
    }

    public TestTime getByMap(Map<String, Object> map) {
        return testTimeDao.getByMap(map);
    }
}
